﻿using flyfire.IO.Ports;
using Gateway.Kernel.Implements.Communication;
using System;
using System.Collections.Generic;
using System.Text;

namespace SerialPortTest
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            List<SerialPortTransmi> serialPorts = new List<SerialPortTransmi>();

            var s = CustomSerialPort.GetPortNames();
            foreach (var item in s)
            {
                Console.WriteLine(item);
                SerialPortTransmi transmi = new SerialPortTransmi(item);
                if (transmi.Open())
                {
                    transmi.Received += Transmi_Received;
                    serialPorts.Add(transmi);
                    Console.WriteLine("Open success");
                    byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, };
                    if (transmi.Send(data))
                    {
                        Console.WriteLine("Send success");
                    }
                    else
                    {
                        Console.WriteLine("Send fail");
                    } 
                }
                else
                {
                    Console.WriteLine("Open fail");

                }


            }
            Console.WriteLine("Close with any key!");
            Console.Read();
            foreach (var item in serialPorts)
            {
                if (item.Close())
                {
                    Console.WriteLine("Close success");
                }
                else
                {
                    Console.WriteLine("Close fail");
                }

            }
         
        }

        private static void Transmi_Received(byte[] data)
        {
            StringBuilder strBuider = new StringBuilder();
            foreach (var item in data)
            {

                strBuider.Append(((int)item).ToString("X2"));
            }
            Console.WriteLine(strBuider);
        }
    }
}
